<?php

namespace App\Http\Controllers;

use App\Models\Coupons;
use App\Models\Forms;
use App\Models\Member;
use App\Models\RedForm;
use Illuminate\Http\Request;

class FormController extends Controller
{
    public function index(Request $request)
    {
        $_limit = $request->limit;
        $Form = Forms::where('member_id', auth('member')->id())
                    ->offset($_limit[0])
                    ->limit($_limit[1])
                    ->orderBy('id','desc')
                    ->get();
        return response(['data'=>$Form]);
    }

    /**
     * 新增编辑表单
     * @param int $_id
     * @param Request $request
     * @return mixed
     */
    public function edit($_id = 0,Request $request)
    {
        $_data = $request->input();
        if($_id) {
            Forms::where("id",$_id)->update($_data);
        } else {
            $_data['member_id'] = auth('member')->id();
            $Form = Forms::create($_data);
            $_id = $Form->id;
        }
        return response(['data'=>$_id]);
    }

    /**
     * 获取一个表单详情
     */
    public function show($_id)
    {
        $Coupon = Forms::find($_id);
        if($Coupon) {
            $_response = response(['data'=>$Coupon]);
        } else {
            $_response = response(['message'=>'请求数据不存在'], 410);
        }
        return $_response;
    }

    /**
     * 删除表单
     */
    public function destroy($_id)
    {
        if(Forms::destroy($_id)) {
            $_response = response(['data'=>'success']);
        } else {
            $_response = response(['message'=>'删除失败'], 402);
        }
        return $_response;
    }

    /**
     * 提交红包表单
     */
    public function store($_id,Request $request)
    {
        try {
            $_member_id = auth("member")->id();
            if(RedForm::where("red_id",$_id)->where("member_id",$_member_id)->exists()) {
                throw new \Exception('不可重复提交', 402);
            }
            $_form_data = Forms::where("id",$request->form_id)->value("form_data");
            $_form_content = array();
            $_data = $request->data;
            for ($i = 0; $i < count($_form_data); $i++) {
                $_temp_value = $_data["form_" . $i];
                $_form_content[$i] = array(
                    "label" => $_form_data[$i]["label"],
                    "value" => is_array($_temp_value) ? implode("，", $_temp_value) : $_temp_value
                );
            }
            unset($_temp_value);
            $RedForm = RedForm::create([
                "member_id" => auth("member")->id(),
                "form_content" => $_form_content,
                "red_id" => $_id
            ]);
            if(!$RedForm) {
                throw new \Exception('提交失败',402);
            }
            return response(['data'=>'提交成功']);
        }catch (\Exception $e) {
            return response(['message'=>$e->getMessage()],$e->getCode());
        }
    }

    /**
     * 红包提交列表
     */
    public function redList($_red_id,Request $request)
    {
        $_limit = $request->input("limit",[0,10]);
        $RedForm = RedForm::where("red_id",$_red_id)
            ->select("id","member_id","form_content","created_at")
            ->offset($_limit[0])
            ->limit($_limit[1])
            ->get();
        if($RedForm) {
            foreach($RedForm as $item) {
                $item->member = Member::where("id",$item->member_id)->select("nickname","avatar")->first();
                $item->setAppends(['create_time']);
            }
        }
        return response(["data"=>$RedForm]);
    }
}
